//任务管理


//站点id
var siteid;
//分页索引：
var index = 1;

//每页显示数目
var pagenum = 9;

//总数目
var count;
//页数
var pages;

$(document).ready(function() {
	// 当操作下拉框时，把值赋给siteid
	$("#selectwebsite").change(function() {
		siteid = $(this).attr('value');
		count = 0;
		if(siteid == "null"){
			siteid =null;
			$("#nonedata").css("display", "block");
			$("#displaytaskdata").html("");
			//$("#displaytaskdata").css("display","none");
			$("#fenye").css("display","none");			
		}else{
			$("#displaytaskdata").css("display","block");
			$("#fenye").css("display","block");
			onetaskcont(siteid,index,pagenum);
		}
	});
});

//第一次加载时显示的分页 
function fenye(count){
	
	var intcount = parseInt(count);
	var numhtml="";
	var counthtml;
	//计数器
	var num = 10;
	if(count<pagenum){
		pages = 1;		
		numhtml = "<li><a href=\"javascript:void(0)\" >"+1+"</a></li>";
		counthtml = "当前第1页/共1页"+count+"条数据";
	}else{
		if(count%pagenum !=0){
						 
			pages = parseInt(intcount/pagenum);
			
			pages = pages+1
		}else{
			pages = intcount/pagenum;
		}
		
		counthtml = "当前第"+index+"页/共"+pages+"页"+count+"条数据";
		
		pageshu = pages+1;
		
		var dangqianpage = this.index;
		if(dangqianpage < num){
			for(var i=1;i<num;i++){
				if(i<pageshu){
					if(i==this.index){
						numhtml += "<li id=\"fenyexuanzhong\">"+i+"</li>";
					}else{
					numhtml += "<li><a href=\"javascript:void(0)\" onclick=\"indexpage("+i+")\" >"+i+"</a></li>";
					}
				}
				
			}
		}
	}
	
	$("#fenyenum").html(numhtml);
	$("#dangqian_count").html(counthtml);
}

function twofenye(index){
	
	var prev = index-5;
	var next = index+6;
	
	var numhtml="";
	
	counthtml = "当前第"+index+"页/共"+pages+"页"+count+"条数据";
	
	for(var i = prev;i<index;i++){
		if(i>0){
			numhtml += "<li><a href=\"javascript:void(0)\" onclick=\"indexpage("+i+")\" >"+i+"</a></li>";
		}
	}
	
	for(var a=index;a<next;a++){
		if(a<pages+1){
			if(a==index){
				numhtml += "<li id=\"fenyexuanzhong\">"+i+"</li>";
			}else{
				numhtml += "<li><a href=\"javascript:void(0)\" onclick=\"indexpage("+a+")\" >"+a+"</a></li>";
			}	
		}
		
	}
	if(index<3){
		$("#shouye").css("display","none");
		$("#prevpage").css("display","none");
	}else{
		$("#shouye").css("display","block");
		$("#prevpage").css("display","block");
		$("#nextpage").css("display","block");
		$("#lastpage").css("display","block");
	}
	if(index<pages+1 && index>pages-10 ){
		$("#nextpage").css("display","none");
		$("#lastpage").css("display","none");

	}
	
	$("#fenyenum").html(numhtml);
	$("#dangqian_count").html(counthtml);
}

function indexpage(index){	
	this.index = index;
	if(index<9){
		onetaskcont(siteid,index,pagenum);
	}else{
		secondtaskcont(siteid,index,pagenum);
	}
}

function shouye(){
	this.index = 1;
	fenye(count);
	$("#shouye").css("display","none");
	$("#prevpage").css("display","none");
	$("#nextpage").css("display","block");
	$("#lastpage").css("display","block");
	secondtaskcont(siteid,index,pagenum);
}

function lastpage(){
	lastpage = this.pages;
	this.index = lastpage;
	$("#shouye").css("display","block");
	$("#prevpage").css("display","block");
	$("#nextpage").css("display","none");
	$("#lastpage").css("display","none");
	secondtaskcont(siteid,index,pagenum);
}

function prevpage(){
	this.index = index-1;
	secondtaskcont(siteid,index,pagenum);
}

function nextpage(){
	this.index = index+1;
	secondtaskcont(siteid,index,pagenum);

}

function onetaskcont(siteid,index,pagenum) {
	var displayhtml = "<table cellspacing=\"0\" id=\"displaytable\"><tbody><tr><th width=\"15%\" style=\"border-left:1px solid #DBDBDB;\">任务名称</th><th width=\"40%\">任务描述</th>"+
	"<th width=\"15%\">执行策略</th><th width=\"15%\">采集策略</th><th width=\"15%\">操作</th></tr>";
	$.ajax({
		type : "post",
		url : "tool/task/displaytask.jsp",
		dataType : "json",
		data : {
			siteid:siteid,
			index:index,
			pagenum:pagenum
		},
		success : function(responseText, textStatus) {
			var jsondata = eval(responseText);
			for ( var i = 0; i < jsondata.length; i++) {
				num = i;
				displayhtml += "<tr><td class=\"tableleftborder\">"
						+ jsondata[i].taskname
						+ "</td>"
						+ "<td>"
						+ jsondata[i].taskdesc
						+ "</td>"
						+ "<td>"
						+ jsondata[i].Taskpolicy
						+ "</td>"
						+ "<td>"
						+ jsondata[i].Taskanalyse
						+ "</td>"
						+ "<span style=\"display:none\" id=\"taskid_"
						+ i
						+ "\">"
						+ jsondata[i].taskid
						+ "</span>"
						+ "<td><a href=\"javascript:void(0)\"  onclick=\"displaymodifictask("
						+ i
						+ ")\" class=\"modif\">修改</a>&nbsp;"
						+ "<a href=\"javascript:void(0)\" onclick=\"deletetask("
						+ i
						+ ")\">删除</a></td></tr>";
				if(i==jsondata.length-1){
					count = jsondata[i].count;
				
				}
			}	
			displayhtml += "</tbody></table>";
			$("#nonedata").css("display", "none");
			$("#displaytaskdata").html(displayhtml);		
			fenye(count);
		},
	})
}

function secondtaskcont(siteid,index,pagenum) {
	var displayhtml = "<table cellspacing=\"0\" id=\"displaytable\"><tbody><tr><th width=\"15%\" style=\"border-left:1px solid #DBDBDB;\">任务名称</th><th width=\"40%\">任务描述</th>"+
	"<th width=\"15%\">执行策略</th><th width=\"15%\">采集策略</th><th width=\"15%\">操作</th></tr>";
	$.ajax({
		type : "post",
		url : "tool/task/displaytask.jsp",
		dataType : "json",
		data : {
			siteid:siteid,
			index:index,
			pagenum:pagenum
		},
		success : function(responseText, textStatus) {
			var jsondata = eval(responseText);
			for ( var i = 0; i < jsondata.length; i++) {
				num = i;
				displayhtml += "<tr><td class=\"tableleftborder\">"
						+ jsondata[i].taskname
						+ "</td>"
						+ "<td>"
						+ jsondata[i].taskdesc
						+ "</td>"
						+ "<td>"
						+ jsondata[i].Taskpolicy
						+ "</td>"
						+ "<td>"
						+ jsondata[i].Taskanalyse
						+ "</td>"
						+ "<span style=\"display:none\" id=\"taskid_"
						+ i
						+ "\">"
						+ jsondata[i].taskid
						+ "</span>"
						+ "<td><a href=\"javascript:void(0)\"  onclick=\"displaymodifictask("
						+ i
						+ ")\" class=\"modif\">修改</a>&nbsp;"
						+ "<a href=\"javascript:void(0)\" onclick=\"deletetask("
						+ i
						+ ")\">删除</a></td></tr>";
				if(i==jsondata.length-1){
					count = jsondata[i].count;
					
				}
			}	
			displayhtml += "</tbody></table>";
			$("#nonedata").css("display", "none");
			$("#displaytaskdata").html(displayhtml);
			twofenye(index);
		},
	})
}
// 显示添加任务窗口：首先判断，是否选择某站点，如未选择弹窗提示
// 选择则显示添加任务窗口
function displayaddtask() {
	if (siteid == null) {
		alert("请先选择站点再进行相关操作");		
	} else {
		popupDiv('displaynewtask')		
	}
}

// 添加任务操作
function addtask() {

	var taskname = $("#taskname").attr("value");
	var taskdesc = $("#taskdesc").attr("value");
	var taskpolicy = $("#taskpolicy").attr("value");
	var taskanalyse = $("#taskanalyse").attr("value");
	
	taskname = taskname.replace(/(^\s*)|(\s*$)/g,"");
	taskdesc = taskdesc.replace(/(^\s*)|(\s*$)/g,"");
	taskpolicy = taskpolicy.replace(/(^\s*)|(\s*$)/g,"");
	taskanalyse = taskanalyse.replace(/(^\s*)|(\s*$)/g,"");
	
	if(taskname=="" || taskdesc=="" || taskpolicy=="" || taskanalyse==""){
		alert("有为空的选项，无法添加");
	}else{
		$.ajax({
			type : "post",
			url : "tool/task/addtask.jsp",
			dataType : "text",
			data : {
				siteid : siteid,
				taskname : taskname,
				taskdesc : taskdesc,
				taskpolicy : taskpolicy,
				taskanalyse : taskanalyse
			},
			success : function(issuccess, textStatus) {
				alert(issuccess);			
				hideDiv('displaynewtask');
				$("#taskname").val("");
				$("#taskdesc").val("");
				$("#taskpolicy").val("");
				$("#taskanalyse").val("");
				if(index<9){
					onetaskcont(siteid,index,pagenum);
				}else{
					secondtaskcont(siteid,index,pagenum);
				}
			}
		});
	}
}
// 修改任务回显页面
function displaymodifictask(num) {

	var taskid = "#taskid_" + num;
	$.ajax({
		type : "post",
		url : "tool/task/displaymodifictask.jsp",
		dataType : "text",
		data : {
			taskid : $(taskid).text()
		},
		success : function(responseText, textStatus) {
			var jsonresult = eval("(" + responseText + ")");
			$("#modific_taskid").val(jsonresult.taskid);
			$("#modific_taskname").val(jsonresult.taskname);
			$("#modific_taskdesc").val(jsonresult.taskdesc);
			$("#modific_taskpolicy").val(jsonresult.Taskpolicy);
			$("#modific_taskanalyse").val(jsonresult.Taskanalyse);
			popupDiv('displaymodifictask')

		}
	});

}

// 修改任务提交处理
function modifictask(){
	
	var taskname = $("#modific_taskname").attr("value");
	var taskdesc = $("#modific_taskdesc").attr("value");
	var taskpolicy = $("#modific_taskpolicy").attr("value");
	var taskanalyse = $("#modific_taskanalyse").attr("value");
	
	taskname = taskname.replace(/(^\s*)|(\s*$)/g,"");
	taskdesc = taskdesc.replace(/(^\s*)|(\s*$)/g,"");
	taskpolicy = taskpolicy.replace(/(^\s*)|(\s*$)/g,"");
	taskanalyse = taskanalyse.replace(/(^\s*)|(\s*$)/g,"");
	if(taskname=="" || taskdesc=="" || taskpolicy=="" || taskanalyse==""){
		alert("有为空的选项，无法修改");
	}else{
		$.ajax({
			type : "post",
			url : "tool/task/modifictask.jsp",
			dataType : "text",
			data : {
				taskid : $("#modific_taskid").attr("value"),
				taskname : taskname,
				taskdesc : taskdesc,
				taskpolicy : taskpolicy,
				taskanalyse : taskanalyse
			},
			success : function(issuccess, textStatus) {
				alert(issuccess);
				secondtaskcont(siteid,index,pagenum);
				hideDiv('displaymodifictask');
				 
			}
		});
	}

}
//删除任务
function deletetask(num) {
	var taskid = "#taskid_" + num;
	$.ajax({
		type : "post",
		url : "tool/task/deletetask.jsp",
		dataType : "text",
		data : {
			taskid : $(taskid).text()
		},
		success : function(issuccess, textStatus) {
			alert(issuccess);
			//window.location.reload();
			secondtaskcont(siteid,index,pagenum);
			if(index<9){
				onetaskcont(siteid,index,pagenum);
			}else{
				secondtaskcont(siteid,index,pagenum);
			}
			 
		}
	});

}